package com.group18.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.group18.entity.AccountUser;
import com.group18.service.AccountUserService;


/**
 * Servlet implementation class DoUpdateAccount
 */
@WebServlet("/DoUpdateAccount")
public class DoUpdateAccount extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DoUpdateAccount() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		
		int id = Integer.parseInt(request.getParameter("id"));
		String username = request.getParameter("username");
		String state = request.getParameter("state");
		String label = request.getParameter("label");
		String statue = request.getParameter("statue");
		double money = Double.parseDouble(request.getParameter("money"));
		String date = request.getParameter("date");
		int family_id = Integer.parseInt(request.getParameter("family_id"));
		int member_id = Integer.parseInt(request.getParameter("member_id"));
		
		//修改数据
		AccountUser au = new AccountUser();
		au.setId(id);
		au.setUsername(username);
		au.setState(state);
		au.setLabel(label);
		au.setStatue(statue);
		au.setMoney(money);
		au.setDate(date);
		au.setFamily_id(family_id);
		au.setMember_id(member_id);
		 
		AccountUserService aus = new AccountUserService();
		int i = aus.upDate(au);
		
		String msg = "";
		if(i > 0) {
			msg = "编辑成功";
			
			String text = request.getParameter("text");
			HttpSession session = request.getSession();
			session.removeAttribute("text");
			if(text != null && !text.equals("")) {
				request.setAttribute("text", text);
				request.setAttribute("msg", msg);
				request.getRequestDispatcher("/DoSearchAccount").forward(request, response);
			}else {
				request.setAttribute("msg", msg);
				request.getRequestDispatcher("/ToIndex").forward(request, response);
			}
			
		}else {
			msg = "编辑失败";
			request.setAttribute("msg", msg);
			request.getRequestDispatcher("/WEB-INF/jsp/addAccount.jsp").forward(request, response);
		}
	}

}
